<?php

/**
 * Description of memcache_client
 *
 * @author phuchv
 */
class memcached_client extends Memcache {
    
    public $memcachedObj = NULL;
    function cache($module = NULL) {
        $MEMCACHED_SERVERS = array(MEMCACHED_IP, //add more server ip here
        );
        if (class_exists('Memcache')) {
            $memcachedObj = new Memcache;
            foreach ($MEMCACHED_SERVERS as $server) {
                switch ($module) {
                    case CacheModule::NEWS:
                        $memcachedObj->connect($server,MEMCACHED_NEWS_PORT); //addServer($server, MEMCACHED_NEWS_PORT);
                        break;
                    case CacheModule::FAQ:
                        $memcachedObj->connect($server,MEMCACHED_FAQ_PORT); //addServer($server, MEMCACHED_FAQ_PORT);
                        break;
                    case CacheModule::ADV:
                        $memcachedObj->connect($server,MEMCACHED_ADV_PORT); //addServer($server, MEMCACHED_ADV_PORT);
                        break;
                    case CacheModule::NEWS_AUTO:
                        $memcachedObj->connect($server, MEMCACHED_NEWS_AUTO_PORT);
                        break;
                    case CacheModule::CITY:
                        $memcachedObj->connect($server, MEMCACHED_CITY_PORT);
                        break;
                    default:
                        $memcachedObj->connect($server); //addServer($server);
                        break;
                }
            }
        } else {
            return false;
        }
        return $memcachedObj;
    }

    function flushCache() {
        if ($memcachedObj == NULL) {
            cache();
        }
        return $memcachedObj->flush();
    }

    function stats() {
        if ($memcachedObj == NULL) {
            cache();
        }
        return $memcachedObj->getExtendedStats();
    }

}

final class CacheModule {

    private function __construct() {}

    const NEWS = 0; // Tin chi tiết
    const FAQ = 1; // Hỏi đáp
    const ADV = 2; // Rao vặt
    const LASTEST_NEWS = 3; // Tin mới nhất
    const FEATURE_NEWS = 4; // Tin nổi bật
    const NEWS_CATE = 5; // News Category
    const CITY = 6; // city list
    const NEWS_AUTO = 7; // News feed auto
}

/* 
    $testArray = array('horse', 'cow', 'pig');
    $strSet = base64_encode(serialize($testArray));        
    memcached_client::cache(CacheModule::NEWS)->set("key", $strSet, false, 60); // expire in 60s

    $strGet = memcached_client::cache(CacheModule::NEWS)->get("key");        
    print_r(unserialize(base64_decode($strGet)));  
 */

?>
